package com.like.dialog;

import android.view.Gravity;
import android.view.View;

import com.like.entity.GeneralResponse;
import com.like.entity.ShareReq;
import com.like.hospitalworld.R;
import com.like.likeutils.network.RetrofitCallback;
import com.like.network.InfoService;
import com.like.network.RetrofitUtil;
import com.like.utils.Constants;
import com.like.widget.MyToast;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;

import retrofit2.Call;
import retrofit2.Response;

@ContentView(R.layout.dialog_share)
public class ShareDialogFragment extends BaseDialogFragment {

    private int mInfoId = 0;
    private InfoService mInfoService;

    @Override
    protected void onSetPosition() {
        setPosition(MATCH_PARENT, WRAP_CONTENT, Gravity.BOTTOM);
    }

    @Override
    protected void onCreateView() {
        super.onCreateView();
        mInfoService = RetrofitUtil.getService(InfoService.class);
    }

    public void setInfoId(int infoId) {
        this.mInfoId = infoId;
    }

    @Event({R.id.share_wx, R.id.share_friend_ship, R.id.share_wx_collect, R.id.share_sina, R.id.share_qq, R.id.share_qq_zone})
    private void shareClick(View v) {
        ShareReq req = new ShareReq(m.getLoginResp().userID, mInfoId, 0);
        switch (v.getId()) {
            case R.id.share_wx:
                req.shareType = ShareReq.SHARE_TYPE_WX;
                break;
            case R.id.share_sina:
                req.shareType = ShareReq.SHARE_TYPE_WB;
                break;
            case R.id.share_friend_ship:
            case R.id.share_wx_collect:
            case R.id.share_qq_zone:
                req.shareType = ShareReq.SHARE_TYPE_OTHERS;
                break;
        }
        Call<GeneralResponse<Object>> call = mInfoService.share(Constants.getGUID(), m.getToken(), req);
        call.enqueue(new RetrofitCallback<GeneralResponse<Object>>() {
            @Override
            public void onResponse(Call<GeneralResponse<Object>> call, Response<GeneralResponse<Object>> response) {
                if (!response.isSuccessful())
                    return;
                GeneralResponse res = response.body();
                if (res.result == 1) {
                    MyToast.showBlueCircleToast(m.mContext, "分享成功");
                } else {
                    MyToast.showBlueCircleToast(m.mContext, "分享失败");
                }
                dismiss();
            }

            @Override
            public void onFailure(Call<GeneralResponse<Object>> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

}
